package com.lsmw.management.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.lsmw.commonutils.R;
import com.lsmw.management.entity.LsRoom;
import com.lsmw.management.entity.LsRoomImg;
import com.lsmw.management.service.LsRoomImgService;
import com.lsmw.management.service.LsRoomService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 二号男嘉宾
 * @since 2020-12-14
 */
@CrossOrigin
@Api(tags = "房间信息")
@RestController
@RequestMapping("/management/room")
public class LsRoomController {

    private LsRoomService lsRoomService;
    private LsRoomImgService lsRoomImgService;

    @Autowired
    public LsRoomController(LsRoomService lsRoomService, LsRoomImgService lsRoomImgService) {
        this.lsRoomService = lsRoomService;
        this.lsRoomImgService = lsRoomImgService;
    }

    @ApiOperation(value = "根据酒店ID获取酒店的所有房间")
    @GetMapping("/fromHotel/{id}")
    public R getRoomListByHotelId(
            @ApiParam(name = "id", value = "酒店id", required = true)
            @PathVariable String id) {
        return lsRoomService.getRoomListByHotelId(id);
    }

    @ApiOperation(value = "根据酒店id添加房间")
    @PostMapping("/id")
    public R addRoom(
            @ApiParam(name = "lsRoomInfo", value = "房间数据(json)", required = true)
            @RequestBody LsRoom lsRoom) {
        return lsRoomService.addRoomByHotelId(lsRoom);
    }

    @ApiOperation(value = "根据酒店名添加房间")
    @PostMapping("/name")
    public R addRoomByHotelName(
            @ApiParam(name = "lsRoomInfo", value = "房间数据(json)", required = true)
            @RequestBody LsRoom lsRoom) {
        try {
            List<HashMap<String, String>> list = new ObjectMapper().readValue(lsRoom.getImg(), ArrayList.class);
            if (!lsRoom.getImg().equals("[]"))
                lsRoom.setImg(list.get(0).get("url"));
            else
                lsRoom.setImg("");
            if (lsRoomService.addRoomByHotelName(lsRoom).getCode() == 400) {
                return R.invalidRequest().data("msg","请求有误，请检查酒店是否存在！");
            }
            if (lsRoom.getImg() != null && lsRoom.getImg() != "") {
                Long roomId = lsRoom.getRoomId();
                List<LsRoomImg> lsRoomImgList = new ArrayList<>();
                for (HashMap<String, String> map : list) {
                    LsRoomImg lsRoomImg = new LsRoomImg();
                    lsRoomImg.setImg(map.get("url"));
                    lsRoomImg.setRoomId(roomId);
                    lsRoomImgList.add(lsRoomImg);
                }
                return lsRoomImgService.saveBatch(lsRoomImgList) ? R.ok() : R.invalidRequest();
            }
            return R.ok();
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            return R.invalidRequest().data("msg", "图片上传有误，请刷新页面重试");
        }
    }

    @ApiOperation(value = "根据ID获取房间信息")
    @GetMapping("/{id}")
    public R getHotelById(
            @ApiParam(name = "id", value = "房间id", required = true)
            @PathVariable String id) {
        LsRoom lsRoom = lsRoomService.getById(id);
        List<LsRoomImg> maps = lsRoomImgService.list(new LambdaQueryWrapper<LsRoomImg>().eq(LsRoomImg::getRoomId, id));
        if (!maps.isEmpty()) {
            List<HashMap<String, String>> toJson = new ArrayList<>();
            for (LsRoomImg lsRoomImg : maps) {
                HashMap<String, String> map = new HashMap<>();
                map.put("uid", lsRoomImg.getImgId().toString());
                map.put("url", lsRoomImg.getImg());
                toJson.add(map);
            }
            return R.ok().data("row", lsRoom).data("img", toJson);
        }
        return R.ok().data("row", lsRoom).data("img", "");
    }

    @ApiOperation(value = "根据ID修改房间信息")
    @PutMapping("/{id}")
    public R updateRoomById(
            @ApiParam(name = "id", value = "房间id", required = true)
            @PathVariable String id,
            @ApiParam(name = "lsHotelInfo", value = "房间数据(json)", required = true)
            @RequestBody LsRoom lsRoom) {
        try {
            List<HashMap<String, String>> list = new ObjectMapper().readValue(lsRoom.getImg(), ArrayList.class);
            if (!lsRoom.getImg().equals("[]"))
                lsRoom.setImg(list.get(0).get("url"));
            else {
                lsRoom.setImg("");
                lsRoomImgService.remove(new LambdaQueryWrapper<LsRoomImg>().eq(LsRoomImg::getRoomId, id));
            }
            if (lsRoomService.updateRoomById(id, lsRoom).getCode() == 404) {
                return R.invalidRequest().data("msg", "请求有误");
            }
            if (lsRoom.getImg() != null && lsRoom.getImg() != "") {
                List<LsRoomImg> lsRoomImgList = new ArrayList<>();
                for (HashMap<String, String> map : list) {
                    LsRoomImg lsRoomImg = new LsRoomImg();
                    lsRoomImg.setImg(map.get("url"));
                    lsRoomImg.setRoomId(Long.valueOf(id));
                    lsRoomImgList.add(lsRoomImg);
                }
                lsRoomImgService.remove(new LambdaQueryWrapper<LsRoomImg>().eq(LsRoomImg::getRoomId, id));
                return lsRoomImgService.saveBatch(lsRoomImgList) ? R.ok() : R.invalidRequest();
            }
            return R.ok();
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            return R.invalidRequest().data("msg", "图片上传有误，请刷新页面重试");
        }
    }

    @ApiOperation(value = "根据ID删除房间")
    @DeleteMapping("/{id}")
    public R removeRoomById(
            @ApiParam(name = "id", value = "房间id", required = true)
            @PathVariable String id) {
        if (id == null || id == "") {
            return R.invalidRequest().data("msg", "请求有误");
        }
        lsRoomImgService.remove(new LambdaQueryWrapper<LsRoomImg>().eq(LsRoomImg::getRoomId, id));
        return lsRoomService.removeRoomById(id);
    }

    @ApiOperation(value = "分页查询")
    @GetMapping("/pageRoom/{current}/{limit}")
    public R getRoomListPage(
            @ApiParam(name = "current", value = "当前页下标", required = true) @PathVariable long current,
            @ApiParam(name = "limit", value = "每页记录数", required = true) @PathVariable long limit) {
        Page<LsRoom> roomPage = new Page<>(current, limit);
        lsRoomService.page(roomPage, null);
        Map map = new HashMap();
        map.put("total", roomPage.getTotal());
        map.put("rows", roomPage.getRecords());
        return R.ok().data(map);
    }

    @ApiOperation(value = "根据name查询房间信息")
    @GetMapping("/{name}/{current}/{limit}")
    public R getRoomInfoById(
            @ApiParam(name = "name", value = "房间name", required = true) @PathVariable String name,
            @ApiParam(name = "current", value = "当前页下标", required = true) @PathVariable long current,
            @ApiParam(name = "limit", value = "每页记录数", required = true) @PathVariable long limit) {
        Page<LsRoom> roomPage = new Page<>(current, limit);
        if (name.equals(Character.toString('0'))) {
            lsRoomService.page(roomPage, null);
        } else {
            lsRoomService.page(roomPage, new LambdaQueryWrapper<LsRoom>().like(LsRoom::getName, name));
        }
        return R.ok().data("rows", roomPage.getRecords()).data("total", roomPage.getTotal());
    }

}

